package priv.pront.code.lanqiao.competition.course.date0327;

import java.util.Arrays;

public class BinaryTest_gg {

    static int[] c;
    int n;

    public BinaryTest_gg(int n) {
        this.n = n;
        c = new int[n + 1];
    }

    public int lowBit(int x){
        return x & -x;
    }

    /**
     * 更新一个元素
     * @param p 更新第p个元素，索引从1开始
     * @param d 增加的之，不是更新过后的值
     */
    public void update(int p, int d) {
        for (; p <= n; p += lowBit(p)) {
            c[p] += d;
        }
    }

    public int sum(int p){
        int ans = 0;
        for (; p > 0; p -= lowBit(p)) {
            ans += c[p];
        }
        return ans;
    }

    public int sum(int left, int right) {
        if (left > n || right < 1 || left > right || left < 1 || right > n) {
            return -1;
        }
        return sum(right) - sum(left - 1);
    }

}
